package com.codemonkey.lq.qd.utils;

import org.apache.ibatis.parsing.XNode;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

public final class XNodeUtils {

	// linux下是\\n ; 所以我们直接写死吧..
	private static final String lineSeparator = "\n\t";//System.getProperty("line.separator", "\n");

	private XNodeUtils() {
		super();
		throw new Error("does not initialize this class");
	}

	/**
	 * 获取节点中包含的文本信息
	 * @sample 获取其中的 ${selectResult.age} == 30
	 * 	 <flow from="step1" to="step2">
	 *		<![CDATA[
	 *			${selectResult.age} == 30
	 * 		   ]]>
	 *	 </flow>
	 * @param node
	 * @return
	 */
	public static String getStrBody(XNode node) {
		XNode flowNode = node;// mapper_ROOT_NODE.evalNode("flow");
		NodeList children = flowNode.getNode().getChildNodes();
		for (int i = 0; i < children.getLength(); i++) {
			XNode child = flowNode.newXNode(children.item(i));
			//String nodeName = child.getNode().getNodeName();
			if (child.getNode().getNodeType() == Node.CDATA_SECTION_NODE
					|| child.getNode().getNodeType() == Node.TEXT_NODE) {
				String data = child.getStringBody("").trim();
				if (lineSeparator.equals(data) || "".equals(data)) {
					continue;
				} else {
					return data;
				}
				// String stringBody = child.getStringBody("");
				// System.out.println(stringBody);
			}
		}
		return "";

	}
}
